package com.hwh.traffic.utils;

import android.content.Context;
import android.content.Intent;

import com.baidu.mapapi.model.LatLng;
import com.hwh.traffic.activity.BusInfoActivity;
import com.hwh.traffic.busApiEntity.NewBusApi;
import com.hwh.traffic.db.TrafficLab;

import org.apache.commons.lang3.StringUtils;

public class BusApiUtil {
    /**
     * @param routeName 路线名称
     * @return 路线的正向ID和反向ID
     */
    public static Long[] getRouteIdByRouteName(String routeName, TrafficLab trafficLab) {
        Long route_id = 0l;
        Long opposite_id = 0l;
        //TrafficLab trafficLab = mapApplication.getTrafficLab();
        if (trafficLab != null) {
            StringUtils.replace(routeName,"路","");
            Long[] routeIds = trafficLab.findRouteIdAndOppositeIdByRouteName(routeName);
            if (routeIds == null) {
                return null;
            }
            route_id = routeIds[0];
            System.out.println("正向路线ID" + route_id);
            opposite_id = routeIds[1];
            System.out.println("反向路线ID" + opposite_id);
        }
        return new Long[]{route_id, opposite_id};

    }

    /**
     * @param routeName
     * @return 获取实时公交的接口url
     */
    public static String[] getBusApi(String routeName, TrafficLab trafficLab, LatLng latLng) {
        Long[] routeIds = getRouteIdByRouteName(routeName,trafficLab);
        if (routeIds == null) {
            return null;
        }
        Long routeId = routeIds[0];
        Long oppositeId = routeIds[1];
        String forw_busApi = "https://app.ibuscloud.com/v11/bus/getBusPositionByRouteId?" + new NewBusApi(String.valueOf(routeId), latLng).toString();
        String oppo_busApi = "https://app.ibuscloud.com/v11/bus/getBusPositionByRouteId?" + new NewBusApi(String.valueOf(oppositeId), latLng).toString();
        return new String[]{forw_busApi, oppo_busApi};

    }

    public static void toBusInfoList(LatLng latLng, Context context,String routeName,TrafficLab trafficLab){
        if (ButtonUtil.isFastDoubleClick()) {
            return;
        }
        String [] busApi = BusApiUtil.getBusApi(routeName,trafficLab,latLng);
        Intent intent = new Intent(context, BusInfoActivity.class);
        intent.putExtra("BUS_API", busApi);
        intent.putExtra("ROUTE_NAME", routeName);
        context.startActivity(intent);

    }

}
